{include file="public/base" /}
{block name="main"}
<link rel="stylesheet" href="__PUBLIC__/admin/layui/css/layui.css"  media="all">
<style>
    .file {
        position: relative;
        display: inline-block;
        background: #D0EEFF;
        border: 1px solid #99D3F5;
        border-radius: 4px;
        padding: 4px 12px;
        overflow: hidden;
        color: #1E88C7;
        text-decoration: none;
        text-indent: 0;
        line-height: 20px;
    }

    .file input {
        position: absolute;
        font-size: 100px;
        right: 0;
        top: 0;
        opacity: 0;
    }

    .file:hover {
        background: #AADFFD;
        border-color: #78C3F3;
        color: #004974;
        text-decoration: none;
    }
    </style>
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>编辑商品</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="close-link">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>
                <div class="ibox-content">
                    <form method="get" action="javascript:;" class="form-horizontal">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">商户名称</label>
                            <div class="col-sm-10">
                                <input type="text" name="shop_name" class="form-control" value="{$goodsInfo['shop_name']}" required>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">主营</label>
                            <div class="col-sm-10">
                                <input type="text" name="main" class="form-control" value="{$goodsInfo['main']}" required>
                            </div>
                        </div>
                      	<div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">地址</label>
                            <div class="col-sm-10">
                                <input type="text" name="address" class="form-control" value="{$goodsInfo['address']}" required>
                            </div>
                        </div>
                      	<div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">联系人</label>
                            <div class="col-sm-10">
                                <input type="text" name="name" class="form-control" value="{$goodsInfo['name']}" required>
                            </div>
                        </div>
                      	<div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">联系人电话</label>
                            <div class="col-sm-10">
                                <input type="text" name="phone" class="form-control" value="{$goodsInfo['phone']}" required>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">轮播图</label>

                            <div class="col-sm-10">
                                <div>
                                    <div class="layui-upload">
                                        <button type="button" class="layui-btn" id="test2">多图片上传</button>
                                        <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                                            预览图：
                                            <div class="layui-upload-list" id="demo3">
                                                {foreach name="$goodsInfo['img']" id="val"}
                                                <img src="{$val}" alt="{$val}" width="220" onclick="del(this,'{$val}',1)" onmouseover="tips(this)">
                                                {/foreach}
                                            </div>
                                        </blockquote>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">简介</label>

                            <div class="col-sm-10">
                                <textarea name="contents" class="form-control" style="height: 100px;">{$goodsInfo['contents']}</textarea>
                            </div>
                        </div>
                       
                        <div id="zhi"></div>
                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-2">
                                <input type="hidden" name="id" value="{$goodsInfo['gid']|default=0}">
                                <button class="btn btn-primary" type="submit" onclick="edit()">保存内容</button>
                                <!--<button class="btn btn-white" type="submit">取消</button>-->
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}
{block name="javascript"}
<script src="ADMIN_JS/content.min.js?v=1.0.0"></script>
<script src="ADMIN_JS/plugins/iCheck/icheck.min.js"></script>
<script src="__PUBLIC__/admin/layui/layui.all.js" charset="utf-8"></script>
<script>
    // var ue = UE.getEditor('editor');
    $("#purchase").on('click', function () {
        clickSwitch()
    });
    function edit()
    {
        //实例化编辑器
        //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例

        var shop_name = $('[name="shop_name"]').val();
        var main = $('[name="main"]').val();
        var address = $('[name="address"]').val();
        var name = $('[name="name"]').val();
        var phone = $('[name="phone"]').val();
        var contents = $('[name="contents"]').val();
        var img = new Array();
        $('[name="img[]"]').each(function(i){
            img.push($('[name="img[]"]').eq(i).val());
        });
        if(shop_name==''){
            layer.msg('请填写商户名称',{icon:6,time:1500});return false;
        }
        if(main==''){
            layer.msg('请填写主营',{icon:6,time:1500});return false;
        }
        if(address==''){
            layer.msg('请填写地址',{icon:6,time:1500});return false;
        }
      	if(name==''){
            layer.msg('请填写联系人',{icon:6,time:1500});return false;
        }
      	if(phone==''){
            layer.msg('请填写联系人电话',{icon:6,time:1500});return false;
        }
        if(img==''){
            var checksImg = '{$goodsInfo["checkImg"]}';
            var arrImg = checksImg.split(',');
            if(arrImg.length==0 || arrImg.length==''){
                layer.msg('请上传商品轮播图片',{icon:6,time:1500});return false;
            }else{
                img = 1;
            }
        }
        var id = $("[name='id']").val();
        if(id!=0){
            var url = '{:url("underline/goodsEdit")}';
        }else{
            var url = '{:url("underline/goodsAdd")}';
        }
        var loading = layer.msg('正在提交数据，请稍后...', {icon: 16,time:6000});
        $.ajax({
            type:'post',
            url:url,
            data:{'id':id,'shop_name':shop_name,'main':main,'address':address,'name':name,'phone':phone,'img':img,'contents':contents},
            success:function(data){
                layer.close(loading);
                if(data.code==1){
                    layer.msg(data.msg,{icon:6,time:1500});
                     setTimeout(function(){window.parent.location.reload();},1000);
                }else{
                    layer.msg(data.msg,{icon:5,time:1500});
                }
            }
        })
    }

</script>
<script>
    layui.use('upload', function() {
        var $ = layui.jquery
            , upload = layui.upload;
        //缩略图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: '{:url("underline/uploadImg")}'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    layer.msg('上传成功');
                    //$("#demoText").append('<input type="hidden" name="small_img"  value=' + res.filename + ' >');
                    $("[name='small_img']").val(res.filename);
                }
                //上传成功
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
        //商品图片上传

        //多图片上传
        upload.render({
            elem: '#test2'
            ,url: '{:url("underline/uploadImg")}'
            ,multiple: true
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo3').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
                });
            }
            ,done: function(res){
                if(res.code > 0){
                    layer.msg('上传成功');
                    $("#zhi").append('<input type="hidden" name="img[]"  value=' + res.filename + ' >');
                }
            }
        });
    });
    function del(index,src,types){
        var id = {$goodsInfo.id};
        if(types==1){
           var url = '{:url("underline/delImg")}';
        }else{
            var url = '{:url("underline/delImgs")}';
        }        $.ajax({
            type:'post',
            url:url,
            data:{'src':src,'id':id},
            success:function(res){
                if(res.code==1){
                    layer.msg(res.msg,{icon:6,time:1500});
                    setTimeout(function(){location.reload();},1000)
                }else{
                    layer.msg(res.msg,{icon:6,time:1500});
                }
            }
        })
    }
    function tips(indexs){
        layer.tips('单击删除', indexs);
    }

</script>
{/block}